<!-- 以下为网站首页 -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome to East China University of Technology Online Judge</title>
    <style>
        /* 起手式, 100%保证我们的样式设置可以不受默认影响 */
        * {
            /* 消除网页的默认外边距 */
            margin: 0px;
            /* 消除网页的默认内边距 */
            padding: 0px;
            /* 去掉a标签的下划线 */
            text-decoration: none;

        }

        html,
        body {
            width: 100%;
            height: 100%;
        }

        .container .navbar {
            width: 100%;
            height: 50px;
            background-color: black;
            /* 给父级标签设置overflow，取消后续float带来的影响 */
            overflow: hidden;
        }

        .container .navbar a {
            /* 设置a标签是行内块元素，允许你设置宽度 */
            display: inline-block;
            /* 设置a标签的宽度,a标签默认行内元素，无法设置宽度 */
            width: 80px;
            /* 设置字体颜色 */
            color: white;
            /* 设置字体的大小 */
            font-size: large;
            /* 设置文字的高度和导航栏一样的高度 */
            line-height: 50px;
            /* 去掉a标签的下划线 */
            text-decoration: none;
            /* 设置a标签中的文字居中 */
            text-align: center;
        }

        /* 设置鼠标事件 */
        .container .navbar a:hover {
            background-color: green;
        }

        .container .navbar .login {
            float: right;
        }

        .container .content {
            /* 设置标签的宽度 */
            width: 800px;
            /* 用来调试 */
            /* background-color: #ccc; */
            /* 整体居中 */
            margin: 0px auto;
            /* 设置文字居中 */
            text-align: center;
            /* 设置上外边距 */
            margin-top: 200px;
        }

        .container .content .font_ {
            /* 设置标签为块级元素，独占一行，可以设置高度宽度等属性 */
            display: block;
            /* 设置每个文字的上外边距 */
            margin-top: 20px;
            /* 去掉a标签的下划线 */
            text-decoration: none;
            /* 设置字体大小
            font-size: larger; */
        }

        .content .login input {
            margin: 10px;
        }

        .content #login {
            padding: 5px;
        }

        .content #signin {
            padding: 5px;
        }

        .content #alteruser {
            padding: 5px;
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 导航栏， 功能不实现-->
        <div class="navbar">
            <a href="/">首页</a>
            <a href="/all_questions">题库</a>
            <a href="#">竞赛</a>
            <a href="#">讨论</a>
            <a href="#">求职</a>
            <a class="login" href="/login">登录</a>
        </div>
        <!-- 网页的内容 -->
        <div class="content">
            <div>
                <img src="/img/ecut5.png" width="134px" height="140px">
            </div>
            <h1 class="font_">登陆注册</h1>
            <div class="login">
                账号:<input type="text" name="username" id="username" value=""><br>
                密码:<input type="password" name="passwd" id="passwd" value=""><br>
                <input type="button" value="登陆" id="login">
                <input type="button" value="注册" id="signin">
                <input type="button" value="忘记密码" id="alteruser">
            </div>
            <div>
                <span id="loginspan"></span>
            </div>
        </div>
        <div style="position:absolute;left:0;bottom: 0;">
            <h5 style="color: black">East China University of Technology Online Judge 1.0</h5>
            <h5 style="color: black;">Copyright © 2023 ecut oj design Team. All Rights Reserved.</h5>
            <h5 style="color: black;">Designer & Developer: 王亚林 欧阳东百 邱全敏</h5>
        </div>
    </div>

    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script>

        var LOGIN_STATUS_ERROR_USERNAME = 10; // 账户错误
        var LOGIN_STATUS_ERROR_PASSWD = 11;   // 密码错误

        var LOGIN_STATUS_ADMIN_SUCCESS = 1; // 管理员登陆成功
        var LOGIN_STATUS_ADMIN_NOW = 2;     // 管理员已经登陆

        var LOGIN_STATUS_USER_SUCCESS = -1; // 用户登陆成功
        var LOGIN_STATUS_USER_NOW = -2;     // 用户已经登陆

        $.ajaxSetup({
            beforeSend(xhr) {
                xhr.setRequestHeader("Authorization", sessionStorage.getItem("token"))
            }
        })

        $("#signin").click(function () {
            location.href = "/signin";
        })

        $("#alteruser").click(function () {
            location.href = "/alteruser";
        })

        $("#login").click(function () {
            var username = $("#username").val();
            var passwd = $("#passwd").val();
            $.ajax({
                type: "post",
                url: "logined",
                datatype: "json",
                data: { "username": username, "passwd": passwd },
                success: function (res) {
                    var status = res.status;
                    var loginspan = $("#loginspan");
                    if (status == LOGIN_STATUS_ERROR_USERNAME) {
                        loginspan.text("账户不存在");
                        return;
                    }

                    if (status == LOGIN_STATUS_ERROR_PASSWD) {
                        loginspan.text("密码错误");
                        return;
                    }

                    if (status == LOGIN_STATUS_ADMIN_SUCCESS) {
                        sessionStorage.setItem("token", res.token);
                        sessionStorage.setItem("pagenumber", 1);
                        sessionStorage.setItem("pagequenumber", 1);
                        location.href = "/login/admin_users/" + username;
                        return;
                    }

                    if (status == LOGIN_STATUS_USER_SUCCESS) {
                        sessionStorage.setItem("token", res.token);
                        location.href = "/login/user/" + username;
                        return;
                    }

                    if (status == LOGIN_STATUS_ADMIN_NOW) {

                        if (confirm("账户已经登陆是否发生跳转")) {
                            sessionStorage.setItem("pagenumber", 1);
                            sessionStorage.setItem("pagequenumber", 1);
                            location.href = "/login/admin_users/" + username;
                        }
                        return;
                    }

                    if (status == LOGIN_STATUS_USER_NOW) {
                        if (confirm("账户已经登陆是否发生跳转")) {
                            location.href = "/login/user/" + username;
                        }
                        return;
                    }
                },
                error: function (res) {
                    alert("登陆错误:" + res.status);
                }
            })
        })
    </script>
</body>

</html>